<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/password.php");
require_once("HTML/QuickForm/submit.php");

/**
 * Change password form
 */
class ChangePasswordPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;

		$l_oldPasswordInput=new HTML_QuickForm_password("oldPassword");
		$l_oldPasswordInput->setLabel("Old password:");
		$this->addElement($l_oldPasswordInput);
		$l_passwordInput=new HTML_QuickForm_password("password");
		$l_passwordInput->setLabel("New password:");
		$this->addElement($l_passwordInput);
		$l_checkPasswordInput=new HTML_QuickForm_password("checkPassword");
		$l_checkPasswordInput->setLabel("New password (confirm):");
		$this->addElement($l_checkPasswordInput);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		
		//The validation rules (either client and server)
		$this->addRule("oldPassword","Old password must not be empty","required",null,"client");
		$this->addRule("password","New password must not be empty","required",null,"client");
		$this->addRule("checkPassword","New password confirmation must not be empty","required",null,"client");
		
		$this->addRule("oldPassword","Old password must not be empty","required",null,"server");
		$this->addRule("password","New password must not be empty","required",null,"server");
		$this->addRule("checkPassword","New password confirmation must not be empty","required",null,"server");
		$this->addRule(array('password', 'checkPassword'), "The new passwords do not match", "compare", null, 'client');
		$this->registerRule("checkOldPassword","callback","checkOldPassword");
		$this->addRule("oldPassword","The oldPassword is invalid","checkOldPassword",null,"server");
		
		$this->applyFilter("oldPassword","trim");
		$this->applyFilter("password","trim");
		$this->applyFilter("checkPassword","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * Quickform rule which check if the specified password is the right one (= the one currently stored in
 * the database)
 * @param string $l_oldPassword
 * @return boolean true if the rule is valid
 */
function checkOldPassword($p_oldPassword){
	$l_user=User::getFromPrimaryKey(SessionManager::getCurrentLoggedUserId());
    return sha1($p_oldPassword)==$l_user->getCredentials();
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){
    	SessionManager::getInstance()->checkSession();
    	displayTop("Change password process",null,null);
    	echo "<h1>Fill the form to update your password</h1>";
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.
 * Check the login/password
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	$l_user=User::getFromPrimaryKey(SessionManager::getCurrentLoggedUserId());
    	$l_user->setCredentials(sha1($page->exportValue("password")));
    	$l_user->save();
    	
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('ChangePasswordProcess', false);
$controller->addPage(new ChangePasswordPage('changePassword'));

$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();
?>